package com.ruoyi.project.user.order.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.user.host.domain.Host;
import com.ruoyi.project.user.order.domain.Order;
import com.ruoyi.project.user.order.service.OrderService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/user/order")
public class OrderController extends BaseController {
    private String prefix="user/order";

    @Autowired
    private OrderService orderService;


    @RequiresPermissions("user:order:view")
    @GetMapping()
    public String showView(){
        System.out.println("执行了控制器");
        return prefix+"/order";
    }

    /**
     * 显示页面数据
     * @return
     */
    @RequiresPermissions("user:order:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo demo(Order order){
        startPage();
        List<Order> list = orderService.showOrder(order);
        //System.out.println(list);
        return getDataTable(list);
    }
    
    @RequiresPermissions("user:order:add")
    @GetMapping("/add")
    public String showAdd(){
        return prefix+"/add";
    }

    /**
     * 检查主持人是否存在
     * @param orderHostname
     * @return
     */
    @RequiresPermissions("user:host:add")
    @PostMapping("/checkHostname")
    @ResponseBody
    public Long checkHostname(String orderHostname){
        return orderService.checkHostname(orderHostname);
    }
    /**
     * 检查用户是否存在
     * @param orderCustomerName
     * @return
     */
    @RequiresPermissions("user:host:add")
    @PostMapping("/checkName")
    @ResponseBody
    public Long checkName(String orderCustomerName){
        return orderService.checkName(orderCustomerName);
    }
    /**
     * 检查公司是否存在
     * @param orderCompany
     * @return
     */
    @RequiresPermissions("user:host:add")
    @PostMapping("/checkCompany")
    @ResponseBody
    public Long checkCompany(String orderCompany){
        return orderService.checkCompany(orderCompany);
    }
    /**
     * 检查主持人是否有时间
     * @param orderCeremonytime
     * @return
     */
    @RequiresPermissions("user:host:add")
    @PostMapping("/checkCeremonytime")
    @ResponseBody
    public Long checkCeremonytime(Integer orderCeremonytime, String orderHostname, Date orderCeremonydatestart){
        return orderService.checkCeremonytime(orderCeremonytime,orderHostname,orderCeremonydatestart);
    }
    /**
     * 检查主持人是否有时间
     * @param orderCeremonytime
     * @return
     */
    @RequiresPermissions("user:host:edit")
    @PostMapping("/checkCeremonytime2")
    @ResponseBody
    public Long checkCeremonytime2(Integer orderCeremonytime, String orderHostname, Date orderCeremonydatestart,Long orderId){
        return orderService.checkCeremonytime2(orderCeremonytime,orderHostname,orderCeremonydatestart,orderId);
    }
    
    /**
     * 检查电话是否存在
     * @param orderPhone
     * @return
     */
    @RequiresPermissions("user:host:add")
    @PostMapping("/checkPhone")
    @ResponseBody
    public Long checkPhone(String orderPhone,String orderCustomerName){
        return orderService.checkPhone(orderPhone,orderCustomerName);
    }

    /**
     * 新增数据
     * @paramorder
     * @return
     */
    @RequiresPermissions("user:order:add")
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult add(Order order){
        return toAjax(orderService.addOrder(order));
    }

    /**
     * 数据回显
     * @param orderId
     * @param model
     * @return
     */
    @RequiresPermissions("user:order:edit")
    @GetMapping("/edit/{orderId}")
    public String showEdit(@PathVariable("orderId") Long orderId, Model model){
        model.addAttribute("order",orderService.showOrderById(orderId));
        return prefix + "/edit";
    }

    /**
     * 主持人订单修改
     * @param order
     * @return
     */
    @RequiresPermissions("user:order:edit")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult edit(Order order){
        return toAjax(orderService.updateOrder(order));
    }

    /**
     * 主持人订单多条删除
     * @param ids
     * @return
     */
    @RequiresPermissions("user:order:remove")
    @RequestMapping("/remove")
    @ResponseBody
    public AjaxResult remove(Long[] ids)
    {
        return toAjax(orderService.delete(ids));
    }

    /**
     * 主持人表导出
     * @param order
     * @return
     */
    @Log(title = "主持人订单管理", businessType = BusinessType.EXPORT)
    @RequiresPermissions("user:order:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Order order,int orderState)
    {
        List<Order> list = orderService.export(order,orderState);
        ExcelUtil<Order> util = new ExcelUtil<Order>(Order.class);
        return util.exportExcel(list, "主持人订单数据");
    }
    
    /**
     *行id查询
     * @param orderId
     * @param model
     * @return
     */
    @RequiresPermissions("user:order:permissionset")
    @GetMapping("/permissionset/{orderId}")
    public String permissionset(@PathVariable Long orderId, Model model){
        model.addAttribute("order",orderService.showOrderById(orderId));
        return prefix+"/permissionset";
    }

    /**
     * 权限修改
     * @param order
     * @return
     */
    @RequiresPermissions("user:order:permissionset")
    @PostMapping("/permissionset")
    @ResponseBody
    public AjaxResult updatePermissionset(Order  order){
        return toAjax(orderService.updateOrder(order));
    }


}
